<?php

namespace App\Http\Controllers\client;

use App\Http\Controllers\Controller;
use App\Models\Appointment;
use App\Models\Store;
use Illuminate\Http\Request;
date_default_timezone_set('PRC');

class AppointmentController extends Controller
{
    protected $appointmentModel;
    protected $storeModel;

    public function __construct()
    {
        $this->appointmentModel = new Appointment();
        $this->storeModel = new Store();
    }

    public function saveAppointment(Request $request){
        $requestData = $request->all();
        $data = [
            'date' => $requestData['date'],
            'time' => $requestData['time'],
            'remark'=> $requestData['remark'] ?? "",
            'store' => $requestData['store'],
            'treatment' => $requestData['treatment'],
            'openid' => $requestData['openid']
        ];
        $res = $this->appointmentModel->saveInfo($data);

        return $this->success($res,"预约信息已登记");
    }


    public function unfinishedAppointmentList(Request $request){
        $openid = $request->input('openid');
        $res = $this->appointmentModel->getInfoList('openid',$openid);
        $arr = [];
        foreach ($res as $k=>$v){
            if(strtotime($v['date']." ".$v['time']) > time() ){
                $storeInfo = $this->getStoreInfoByName($v['store']);
                $arr[$k]['id'] = $v['id'];
                $arr[$k]['time'] = $v['time'];
                $arr[$k]['date'] = $v['date'];
                $arr[$k]['remark'] = $v['remark'];
                $arr[$k]['store'] = $v['store'];
                $arr[$k]['treatment'] = $v['treatment'];
                $arr[$k]['openid'] = $v['openid'];
                $arr[$k]['address'] = $storeInfo['address'];
                $arr[$k]['phone'] = $storeInfo['phone'];
            }
        }


        return $this->success($arr,'未完成的预约获取成功');
    }

    public function getStoreInfoByName($name){

        return $this->storeModel->getInfo("name",$name);
    }
}
